<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('标的公司名称(全称)')" prop="companyName">
            <el-autocomplete
              v-if="!form.id"
              v-model="form.companyName"
              class="inline-input name"
              :fetch-suggestions="companyQuerySearch"
              :placeholder="$t('标的公司名称(全称)')"
              :trigger-on-focus="false"
              :maxlength="100"
              @select="companyHandleSelect"
            >
              <template v-if="enableTyc" slot="append">
                <a @click="companyHandleSelect">{{ $t('查询工商信息') }}</a>
              </template>
            </el-autocomplete>
            <el-input v-else v-model="form.companyName" readonly show-word-limit maxlength="100">
              <template v-if="enableTyc" slot="append">
                <a @click="companyHandleSelect">{{ $t('查询工商信息') }}</a>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司简称')" prop="companyShortName">
            <el-input v-model="form.companyShortName" :placeholder="$t('公司简称')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开业时间')" prop="openTime">
            <DatePicker v-model="form.openTime" :placeholder="$t('开业时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('企业状态')" prop="enterpriseStage">
            <el-input v-model="form.enterpriseStage" :placeholder="$t('企业状态')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('法定代表人')" prop="legalPerson">
            <el-input v-model="form.legalPerson" :placeholder="$t('法定代表人')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('企业统一社会信用代码')" prop="companyCode">
            <el-input v-model="form.companyCode" :placeholder="$t('企业统一社会信用代码')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <Industry ref="industryZj" :buss-data="form" />
        <Area ref="cityZj" :buss-data="form" />
        <el-col :span="24">
          <el-form-item :label="$t('注册地-详细地址')" prop="registAddress">
            <el-input v-model="form.registAddress" :placeholder="$t('注册地-详细地址')" show-word-limit :maxlength="250" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('注册资本金额') + $t('金额单位')" prop="registCapital">
            <Money v-model="form.registCapital" unit="w" :placeholder="$t('注册资本金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('注册资本币种')" prop="registCurrency">
            <dictSelect
              v-model="form.registCurrency"
              :clearable="true"
              dictionary="currency"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('经营范围')" prop="bussScope">
            <el-input v-model="form.bussScope" type="textarea" :placeholder="$t('经营范围')" show-word-limit :maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('企业简介')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('企业简介')" show-word-limit :maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-show="!btnHide" slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="company:subsidiary" :save-show="sourceType !== 'bussRisk'" />
    </div>
  </div>
</template>

<script>
    import { saveCompanySubsidiary, getCompanySubsidiary } from '@/api/company/subsidiary/companySubsidiary'
    import { companyDetailsByName, companyListByName } from '@/api/company/base/companyBase'
    // 行业组件
    import Industry from '@/components/Industry'
    // 省市组件
    import Area from '@/components/Area'
    export default {
        components: {
          Industry,
          Area
        },
        props: {
          sourceType: {
            type: String,
            default: ''
          }
        },
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  companyName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  companyShortName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                },
                // 是否隐藏保存等按钮
                btnHide: false,
                // 天眼查查询条件
                tycParam: {
                  companyName: '',
                  companyType: 'childCompany',
                  id: this.$route.query.companyId
                },
                enableTyc: undefined
            }
        },
        mounted() {
          this.$bus.$emit('async_done')
          this.enableTyc = (window.localStorage.getItem('enableTyc') === 'true')
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getCompanySubsidiary(data.id).then(response => {
                        this.form = response.data
                        this.$nextTick(() => {
                          this.$refs.industryZj.initIndustrySecond()
                          this.$refs.cityZj.initCity()
                          this.$refs.cityZj.initDistrict()
                        })
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
                this.form.companyId = this.$route.query.companyId
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                companyId: undefined,
                tycId: undefined,
                companyName: undefined,
                companyShortName: undefined,
                openTime: undefined,
                enterpriseStage: undefined,
                legalPerson: undefined,
                companyCode: undefined,
                registCapital: undefined,
                registCurrency: undefined,
                industryFirst: undefined,
                industrySecond: undefined,
                provinceRegistered: undefined,
                cityRegistered: undefined,
                districtRegistered: undefined,
                registAddress: undefined,
                bussScope: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                remark: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        this.form.source = '1'
                        saveCompanySubsidiary(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                              if (this.$route.query.companyId) {
                                this.$store.dispatch('bussRisk/UPDATE_BUSS_RISK_COUNT', { id: this.$route.query.companyId })
                              }
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
            // 标的公司查询
            companyQuerySearch(queryString, cb) {
              companyListByName(queryString).then(res => {
                if (res.code === 200) {
                  cb(res.data)
                }
              })
            },
            // 清空form
            formCleaing() {
              this.form.companyShortName = ''
              this.form.creditCode = ''
              this.form.legalPerson = ''
              this.form.setDate = ''
              this.form.industryFirst = ''
              this.form.industrySecond = ''
              this.form.provinceRegistered = ''
              this.form.cityRegistered = ''
              this.form.districtRegistered = ''
              this.form.detailsRegistered = ''
              this.form.capitalRegistered = ''
              this.form.currency = ''
              this.form.tycId = ''
              this.form.logoInfo = ''
            },
            // 标的公司选择后事件
            companyHandleSelect(item) {
              var companyName = this.form.companyName
              if (companyName === '' || companyName === undefined) {
                this.msgError(this.$t('请先输入公司全称！！'))
                return
              }
              this.tycParam.companyName = companyName
              this.tycParam.id = this.form.id
              this.tycParam.tycId = this.form.tycId || item.id
              this.tycParam.orgType = item.type
              companyDetailsByName(this.tycParam).then(res => {
                if (res.code === 200) {
                  if (res.msg === 'exist') {
                    this.form.companyName = ''
                    this.msgError(this.$t('公司全称已存在！！'))
                  } else if (res.msg === 'nofind') {
                    this.form.companyName = ''
                    this.msgError(this.$t('天眼查没有获取到该公司信息！！'))
                  } else {
                    this.formCleaing()
                    // 公司简称
                    this.form.companyShortName = res.data.companyShortName
                    // 公司名称
                    this.form.companyName = res.data.companyName
                    // 企业统一社会信用代码
                    this.form.companyCode = res.data.creditCode
                    // 法人代表
                    this.form.legalPerson = res.data.legalPerson
                    // 成立时间
                    this.form.openTime = res.data.setDate
                    // 注册地-详细地址
                    this.form.registAddress = res.data.detailsRegistered
                    // 注册资本金额
                    this.form.registCapital = res.data.capitalRegistered
                    // 注册资本币种
                    this.form.registCurrency = res.data.currency
                    // 企业简介
                    this.form.remark = res.data.remark
                     // 经营范围
                     this.form.bussScope = res.data.businessScope
                    // 行业
                    this.form.industryFirst = res.data.industryFirst
                    this.form.industrySecond = res.data.industrySecond
                    this.$refs.industryZj.initIndustrySecond()
                    // 注册地-省
                    this.form.provinceRegistered = res.data.provinceRegistered
                    this.$refs.cityZj.initCity()
                    this.form.cityRegistered = res.data.cityRegistered
                    // 注册区
                    this.$refs.cityZj.initDistrict()
                     this.form.districtRegistered = res.data.registerDistrict
                    this.msgSuccess(this.$t('已获取工商信息！'))
                  }
                }
              })
            }
        }

    }
</script>
